var __assign =
  (this && this.__assign) ||
  function () {
    __assign =
      Object.assign ||
      function (t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
        }
        return t;
      };
    return __assign.apply(this, arguments);
  };
var __rest =
  (this && this.__rest) ||
  function (s, e) {
    var t = {};
    for (var p in s)
      if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === 'function')
      for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
          t[p[i]] = s[p[i]];
      }
    return t;
  };
import React, { useRef, useState } from 'react';
import { Form, Row } from 'antd';
import LeggoItem from '../components/LeggoItem';
import { Leggo } from '../utils/Leggo';
var leggoStores = new WeakMap();
export function LeggoForm(props) {
  var _a;
  var leggo = props.leggo,
    onFieldsChange = props.onFieldsChange,
    overlapFormProps = __rest(props, ['leggo', 'onFieldsChange']);
  var _b =
      ((_a = leggoStores.get(leggo.ref)) === null || _a === void 0 ? void 0 : _a.schemaModel) || {},
    formProps = _b.formProps,
    schemaList = _b.schemaList;
  var handleFieldsChange = function (changedFields, allFields) {
    changedFields.forEach(function (_a) {
      var name = _a.name,
        value = _a.value;
      var changedSchema = schemaList.find(function (schema) {
        return schema.getStringedName() === String(name);
      });
      if (changedSchema) {
        changedSchema.currentItemValue = value;
        changedSchema.linkingStringedNames.forEach(function (linkingName) {
          var targetSchema = schemaList.find(function (schema) {
            return schema.getStringedName() === linkingName;
          });
          targetSchema.forceLeggoFormItemRender();
        });
      }
    });
    onFieldsChange === null || onFieldsChange === void 0
      ? void 0
      : onFieldsChange(changedFields, allFields);
  };
  return React.createElement(
    Form,
    __assign({}, formProps, overlapFormProps, { onFieldsChange: handleFieldsChange }),
    React.createElement(
      Row,
      { gutter: 24 },
      schemaList === null || schemaList === void 0
        ? void 0
        : schemaList.map(function (schema) {
            return React.createElement(LeggoItem, {
              key: schema.id,
              leggo: leggo,
              schema: schema,
              schemaList: schemaList
            });
          })
    )
  );
}
LeggoForm.useLeggo = function (schemaModel0, middleware, publicStates) {
  var keyRef = useRef(null);
  var setForceRender = useState(0)[1];
  var leggo = null;
  if (!leggoStores.has(keyRef)) {
    leggo = new Leggo(keyRef, setForceRender, schemaModel0, middleware, publicStates);
    leggoStores.set(keyRef, leggo);
  }
  return leggo || leggoStores.get(keyRef);
};
